<style lang="less">
    @import './login.less';
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                    <p class="login-tip"></p>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import http from '../libs/http';
import axios from 'axios';
import Cookies from 'js-cookie';
export default {
    data () {
        return {
            form: {
                userName: '',
                password: ''
            },
            rules: {
                userName: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        handleSubmit () {
            var that = this;
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    var data = {
                        'username':that.form.userName,
                        'password':that.form.password,
                    }
                    this.apiPost('admin/base/login', data).then((res) => {
                        if (res.code != 200) {
                            console.log(res)

                            that.$Message.error({
                                content: res.error,
                                duration: 10,
                                closable: true
                            });
                        } else {
                            Cookies.set('user', that.form.userName);
                            Cookies.set('password', that.form.password);
                            Cookies.set('access', 0);      
                            that.$store.commit('setAvator', 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3448484253,3685836170&fm=27&gp=0.jpg');
                            Cookies.set('authKey', res.data.authKey);
                            Cookies.set('sessionId', res.data.sessionId)          // 用户sessionid
                            axios.defaults.headers.authKey = Cookies.get('authKey')  //定义头部header默认传递信息
                            axios.defaults.headers.sessionId = Cookies.get('sessionId') //定义头部header默认传递信息
                            that.$router.push({
                                name: 'home_index'
                            });
                        }
                    })
                }
            });
        }
    },
    mixins: [http]
};
</script>

<style>

</style>
